using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;

namespace Publicuse.Entity
{
  /// <summary>
  /// 获取请求头实例
  /// </summary>
  public static class HttpContextExtendBase
  {
    private static IHttpContextAccessor _accessor;

    private static ConcurrentDictionary<string, JwtToken> Tokens =
      new ConcurrentDictionary<string, JwtToken>();

    public static HttpContext Current => _accessor.HttpContext;

    public static IApplicationBuilder UseStaticHttpContext(this IApplicationBuilder app)
    {
      var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
      HttpContextExtendBase.Configure(httpContextAccessor);
      return app;
    }

    internal static void Configure(IHttpContextAccessor accessor)
    {
      _accessor = accessor;
    }

    public static bool havetoken(string token)
    {
      return Tokens.ContainsKey(token);
    }

    public static void AddJwtToken(string token, JwtToken jwtToken)
    {
      if (Tokens.ContainsKey(token))
      {
        Tokens.TryRemove(token, out var tkv);
        Tokens.TryAdd(token, jwtToken);
      }
      else
      {
        Tokens.TryAdd(token, jwtToken);
      }
    }

    public static void RemoveToken(string token)
    {
      if (Tokens.ContainsKey(token))
      {
        Tokens.TryRemove(token, out var tkv);
      }
    }

    public static void ClearToken()
    {
      Tokens.Clear();
    }

    public static string GetLoginAppid(string loginToken = "")
    {
      var jwttoken = HttpContextExtendBase.Current?.Request.Headers["token"].ToString();
      if (string.IsNullOrEmpty(jwttoken))
      {
        if (!string.IsNullOrEmpty(loginToken))
        {
          jwttoken = loginToken;
        }
      }
      if (!string.IsNullOrEmpty(jwttoken))
      {
        JwtToken token = new JwtToken();
        GetJwtToken(jwttoken, out token);
        if (token != null)
        {
          return token.appid;
        }
        else
        {
          return string.Empty;
        }
      }
      else
      {
        return string.Empty;
      }
    }

    public static string GetLoginrId(string loginToken = "")
    {
      var jwttoken = HttpContextExtendBase.Current?.Request.Headers["token"].ToString();
      if (string.IsNullOrEmpty(jwttoken))
      {
        if (!string.IsNullOrEmpty(loginToken))
        {
          jwttoken = loginToken;
        }
      }
      if (!string.IsNullOrEmpty(jwttoken))
      {
        JwtToken token = new JwtToken();
        GetJwtToken(jwttoken, out token);
        if (token != null)
        {
          return token.rId;
        }
        else
        {
          return string.Empty;
        }
      }
      else
      {
        return string.Empty;
      }
    }

    /// <summary>
    /// 获取登录企业名称
    /// </summary>
    /// <param name="loginToken"></param>
    /// <returns></returns>
    public static string GetLogincName(string loginToken = "")
    {
      var jwttoken = HttpContextExtendBase.Current?.Request.Headers["token"].ToString();
      if (string.IsNullOrEmpty(jwttoken))
      {
        if (!string.IsNullOrEmpty(loginToken))
        {
          jwttoken = loginToken;
        }
      }
      if (!string.IsNullOrEmpty(jwttoken))
      {
        JwtToken token = new JwtToken();
        GetJwtToken(jwttoken, out token);
        if (token != null)
        {
          return token.cName;
        }
        else
        {
          return string.Empty;
        }
      }
      else
      {
        return string.Empty;
      }
    }

    public static string GetLoginRsId(string loginToken = "")
    {
      var jwttoken = HttpContextExtendBase.Current?.Request.Headers["token"].ToString();
      if (string.IsNullOrEmpty(jwttoken))
      {
        if (!string.IsNullOrEmpty(loginToken))
        {
          jwttoken = loginToken;
        }
      }
      if (!string.IsNullOrEmpty(jwttoken))
      {
        JwtToken token = new JwtToken();
        GetJwtToken(jwttoken, out token);
        if (token != null)
        {
          return token.rsId;
        }
        else
        {
          return string.Empty;
        }
      }
      else
      {
        return string.Empty;
      }
    }

    public static string GetLoginRsName(string loginToken = "")
    {
      var jwttoken = HttpContextExtendBase.Current?.Request.Headers["token"].ToString();
      if (string.IsNullOrEmpty(jwttoken))
      {
        if (!string.IsNullOrEmpty(loginToken))
        {
          jwttoken = loginToken;
        }
      }
      if (!string.IsNullOrEmpty(jwttoken))
      {
        JwtToken token = new JwtToken();
        GetJwtToken(jwttoken, out token);
        if (token != null)
        {
          return token.rsName;
        }
        else
        {
          return string.Empty;
        }
      }
      else
      {
        return string.Empty;
      }
    }

    public static string GetLoginDepType(string loginToken = "")
    {
      var jwttoken = HttpContextExtendBase.Current?.Request.Headers["token"].ToString();
      if (string.IsNullOrEmpty(jwttoken))
      {
        if (!string.IsNullOrEmpty(loginToken))
        {
          jwttoken = loginToken;
        }
      }
      if (!string.IsNullOrEmpty(jwttoken))
      {
        JwtToken token = new JwtToken();
        GetJwtToken(jwttoken, out token);
        if (token != null)
        {
          return token.dtype;
        }
        else
        {
          return string.Empty;
        }
      }
      else
      {
        return string.Empty;
      }
    }

    /// <summary>
    /// 获取登录用户编码
    /// </summary>
    /// <param name="loginToken"></param>
    /// <returns></returns>
    public static string GetLoginCode(string loginToken = "")
    {
      var jwttoken = HttpContextExtendBase.Current?.Request.Headers["token"].ToString();
      if (string.IsNullOrEmpty(jwttoken))
      {
        if (!string.IsNullOrEmpty(loginToken))
        {
          jwttoken = loginToken;
        }
      }
      if (!string.IsNullOrEmpty(jwttoken))
      {
        JwtToken token = new JwtToken();
        GetJwtToken(jwttoken, out token);
        if (token != null)
        {
          return token.uCode;
        }
        else
        {
          return string.Empty;
        }
      }
      else
      {
        return string.Empty;
      }
    }

    /// <summary>
    /// 获取登录用户名称
    /// </summary>
    /// <param name="loginToken"></param>
    /// <returns></returns>
    public static string GetLoginuName(string loginToken = "")
    {
      var jwttoken = HttpContextExtendBase.Current?.Request.Headers["token"].ToString();
      if (string.IsNullOrEmpty(jwttoken))
      {
        if (!string.IsNullOrEmpty(loginToken))
        {
          jwttoken = loginToken;
        }
      }
      if (!string.IsNullOrEmpty(jwttoken))
      {
        JwtToken token = new JwtToken();
        GetJwtToken(jwttoken, out token);
        if (token != null)
        {
          return token.uName;
        }
        else
        {
          return string.Empty;
        }
      }
      else
      {
        return string.Empty;
      }
    }

    /// <summary>
    /// 获取登录用户角色级别
    /// </summary>
    /// <param name="loginToken"></param>
    /// <returns></returns>
    public static string GetLoginrName(string loginToken = "")
    {
      var jwttoken = HttpContextExtendBase.Current?.Request.Headers["token"].ToString();
      if (string.IsNullOrEmpty(jwttoken))
      {
        if (!string.IsNullOrEmpty(loginToken))
        {
          jwttoken = loginToken;
        }
      }
      if (!string.IsNullOrEmpty(jwttoken))
      {
        JwtToken token = new JwtToken();
        GetJwtToken(jwttoken, out token);
        if (token != null)
        {
          return token.rNameb;
        }
        else
        {
          return string.Empty;
        }
      }
      else
      {
        return string.Empty;
      }
    }

    /// <summary>
    /// 获取登录用户角色名称
    /// </summary>
    /// <param name="loginToken"></param>
    /// <returns></returns>
    public static string GetLoginrNameb(string loginToken = "")
    {
      var jwttoken = HttpContextExtendBase.Current?.Request.Headers["token"].ToString();
      if (string.IsNullOrEmpty(jwttoken))
      {
        if (!string.IsNullOrEmpty(loginToken))
        {
          jwttoken = loginToken;
        }
      }
      if (!string.IsNullOrEmpty(jwttoken))
      {
        JwtToken token = new JwtToken();
        GetJwtToken(jwttoken, out token);
        if (token != null)
        {
          return token.rNameb;
        }
        else
        {
          return string.Empty;
        }
      }
      else
      {
        return string.Empty;
      }
    }

    /// <summary>
    /// 获取登录用户企业编号
    /// </summary>
    /// <param name="loginToken"></param>
    /// <returns></returns>
    public static string GetLoginUsercId(string loginToken = "")
    {
      var jwttoken = HttpContextExtendBase.Current?.Request.Headers["token"].ToString();
      if (string.IsNullOrEmpty(jwttoken))
      {
        if (!string.IsNullOrEmpty(loginToken))
        {
          jwttoken = loginToken;
        }
      }
      if (!string.IsNullOrEmpty(jwttoken))
      {
        JwtToken token = new JwtToken();
        GetJwtToken(jwttoken, out token);
        if (token != null)
        {
          return token.cId;
        }
        else
        {
          return string.Empty;
        }
      }
      else
      {
        return string.Empty;
      }
    }

    /// <summary>
    /// 获取登录用户编号
    /// </summary>
    /// <param name="loginToken"></param>
    /// <returns></returns>
    public static string GetLoginuId(string loginToken = "")
    {
      var jwttoken = HttpContextExtendBase.Current?.Request.Headers["token"].ToString();
      if (string.IsNullOrEmpty(jwttoken))
      {
        if (!string.IsNullOrEmpty(loginToken))
        {
          jwttoken = loginToken;
        }
      }
      if (!string.IsNullOrEmpty(jwttoken))
      {
        JwtToken token = new JwtToken();
        GetJwtToken(jwttoken, out token);
        if (token != null)
        {
          return token.Id;
        }
        else
        {
          return string.Empty;
        }
      }
      else
      {
        return string.Empty;
      }
    }

    /// <summary>
    /// 获取登录用户token字符串
    /// </summary>
    /// <returns></returns>
    public static string GetTokenStr()
    {
      var jwttoken = HttpContextExtendBase.Current?.Request.Headers["token"].ToString();
      if (!string.IsNullOrEmpty(jwttoken))
      {
        return jwttoken;
      }
      else
      {
        return string.Empty;
      }
    }

    /// <summary>
    /// 获取登录用户token信息
    /// </summary>
    /// <param name="token"></param>
    /// <param name="jwtToken"></param>
    public static void GetJwtToken(string token, out JwtToken jwtToken)
    {
      jwtToken = null;
      if (Tokens != null && Tokens.Count > 0 && !string.IsNullOrEmpty(token))
      {
        Tokens.TryGetValue(token, out jwtToken);
      }
    }

    /// <summary>
    /// 获取登录用户系统模块mingc
    /// </summary>
    /// <param name="loginToken"></param>
    /// <returns></returns>
    public static string GetLoginSysName(string loginToken = "")
    {
      var jwttoken = HttpContextExtendBase.Current?.Request.Headers["token"].ToString();
      if (string.IsNullOrEmpty(jwttoken))
      {
        if (!string.IsNullOrEmpty(loginToken))
        {
          jwttoken = loginToken;
        }
      }
      if (!string.IsNullOrEmpty(jwttoken))
      {
        JwtToken token = new JwtToken();
        GetJwtToken(jwttoken, out token);
        if (token != null)
        {
          return token.sName;
        }
        else
        {
          return string.Empty;
        }
      }
      else
      {
        return string.Empty;
      }
    }
  }
}
